package com.lishe.lishe.lishe.module.mall.adapter;


import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lishe.lishe.Imageloader.ImageLoaderUtils;
import com.lishe.lishe.R;
import com.lishe.lishe.lishe.MyRecyclerView.RecyclerviewBaseAdapter;
import com.lishe.lishe.lishe.module.mall.bean.category.CategoryFirst;
import com.lishe.lishe.lishe.utils.Utils;

import java.util.List;

/**
 * Created by yang on 2017/11/6.
 */

public class Classification2Adapter extends RecyclerviewBaseAdapter {
    private Context mContext;
    private List<CategoryFirst> mList;
    private int type=1;
    private int typePosition;
    private ImageLoaderUtils utils;
    private int mDisplayWidth;


    public Classification2Adapter(Context context, List<CategoryFirst> list,int displayWidth){
        this.mContext=context;
        this.mList=list;
        this.mDisplayWidth=displayWidth;
        utils=new ImageLoaderUtils();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolderMy(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(mContext).inflate(R.layout.item_classification2,null);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolderMy(RecyclerView.ViewHolder holder, int position) {
        CategoryFirst category=mList.get(position);
        if(getItemViewTypeMy(position)==type){
            typePosition=position;
            ((MyViewHolder)holder).ll.setVisibility(View.GONE);
        }else{
            ((MyViewHolder)holder).ll.setVisibility(View.VISIBLE);
            utils.displayImage(category.getImg_url(),((MyViewHolder)holder).img);
            ((MyViewHolder)holder).imgText.setText(category.getCate_name());
        }

    }

    @Override
    public int getItemCountMy() {
        return mList.size();
    }

    @Override
    public int getItemViewTypeMy(int position) {
        if(mList.get(position).isSecond()){
            return type;
        }
        return 0;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager instanceof GridLayoutManager) {
            final GridLayoutManager gridManager = ((GridLayoutManager) manager);
            gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    int types = getItemViewType(position);
                    if (types == type) {
                        return gridManager.getSpanCount();
                    }
                    return 1;
                }
            });
        }
    }

    public class MyViewHolder extends RecyclerView.ViewHolder{
        TextView imgText;
        ImageView img;
        LinearLayout ll;

        public MyViewHolder(View itemView) {
            super(itemView);
            int width = (mDisplayWidth - Utils.dip2px(mContext, 200)) / 3;
            imgText=itemView.findViewById(R.id.imgText);
            img=itemView.findViewById(R.id.img);
            img.setLayoutParams(new LinearLayout.LayoutParams(width,width));
            ll=itemView.findViewById(R.id.ll);
        }
    }

}
